package com.yida.wy.vote.outside.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.file.MimeTypeUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.Logical;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.wy.vote.outside.domain.dto.WyVoteTemplateDetailDTO;
import com.yida.wy.vote.outside.domain.request.WyVoteTemplateRequest;
import com.yida.wy.vote.outside.service.IWyVoteTemplateService;
import com.yida.system.api.RemoteFileService;
import com.yida.system.api.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 投票模板Controller
 *
 * @author lqq
 * @date 2023-03-10
 */
@Api(tags = "投票选项模板")
@RestController
@RequestMapping("/template")
public class WyVoteTemplateController extends BaseController {

	@Resource
	private IWyVoteTemplateService voteTemplateService;

	@Resource
	private RemoteFileService remoteFileService;

	@ApiOperation("获取投票模板列详细信息")
	@RequiresPermissions(value = {"gwj:vote:outside:e:template:edit","gwj:vote:outside:g:template:query"},logical = Logical.OR)
	@GetMapping(value = "/{voteId}")
	public AjaxResult getInfo(@PathVariable("voteId") Long voteId) {
		return AjaxResult.success(BeanUtil.copyProperties(voteTemplateService.selectVoteTemplateByVoteId(voteId), WyVoteTemplateDetailDTO.class));
	}

	@ApiOperation("修改投票模板列")
	@RequiresPermissions("gwj:vote:outside:e:template:edit")
	@Log(title = "公维金外网投票-编辑选项", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody WyVoteTemplateRequest request) {
		return toAjax(voteTemplateService.addOrUpdateVoteTemplate(request));
	}

	@ApiOperation("投票项目上传表决项图片")
	@Log(title = "公维金外网投票-上传表决项图片", businessType = BusinessType.UPDATE, systemName = "vote")
	@PostMapping(value = "/uploadImg")
	@RequiresPermissions("gwj:vote:outside:e:template:edit")
	public AjaxResult uploadImg(MultipartFile file) {
		if (file != null && !file.isEmpty()) {
			String extension = FileTypeUtils.getExtension(file);
			if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
				return AjaxResult.error("上传图片失败,图片格式不正确" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
			}
			R<SysFile> fileResult = remoteFileService.upload(file);
			if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
				return AjaxResult.error("上传图片失败,图片服务异常");
			}
			return AjaxResult.success("上传图片成功", fileResult.getData().getUrl());
		}
		return AjaxResult.error("上传图片失败,图片异常");
	}
}
